package com.alexmaodali.service.impl;

import com.alexmaodali.domain.dto.UserQueryDTO;
import com.alexmaodali.domain.po.PageBean;
import com.alexmaodali.domain.po.User;
import com.alexmaodali.mapper.UserMapper;
import com.alexmaodali.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {
    final private UserMapper userMapper;

    /**
     * 查询全部
     *
     * @return
     */
    @Override
    public List<User> getAll() {
        List<User> userList = userMapper.getAll();
        return userList;
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Override
    public User getById(Integer id) {
        User user = userMapper.getById(id);
        return user;
    }

    /**
     * 根据id删除
     *
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        userMapper.deleteById(id);
    }

    /**
     * 根据id禁用
     *
     * @param id
     */
    @Override
    public void disableById(Integer id) {
        userMapper.disableById(id);
    }

    /**
     * 根据id启用
     *
     * @param id
     */
    @Override
    public void enableById(Integer id) {
        userMapper.enableById(id);
    }

    /**
     * 插入用户
     *
     * @param user
     */
    @Override
    public void insertUser(User user) {
        user.setStatus(1);
        user.setDel(1);
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        userMapper.insertUser(user);
    }

    /**
     * 更新用户
     *
     * @param user
     */
    @Override
    public void updateUser(User user) {
        user.setStatus(1);
        user.setDel(1);
        user.setUpdateTime(LocalDateTime.now());
        userMapper.updateUser(user);
    }

    /**
     * 分页查询
     *
     * @param currentPage
     * @param pageSize
     * @param userQueryDTO
     * @return
     */
    @Override
    public PageBean page(Integer currentPage, Integer pageSize,
                         UserQueryDTO userQueryDTO) {
        PageHelper.startPage(currentPage, pageSize);
        List<User> userList = userMapper.page(userQueryDTO);
        Page<User> p = (Page<User>) userList;
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    /**
     * 综合删除
     *
     * @param ids
     */
    @Override
    public void delete(List<Integer> ids) {
        userMapper.delete(ids);
    }

}
